<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<%java.util.Random r=new java.util.Random();  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript">
<!--
	function checkForm2(){
		var objs = document.getElementsByName("id");
		var sign = 0;
		for(var i = 0; i < objs.length; i++){
			if(objs[i].value != 0 && objs[i].checked)sign = sign + 1;
		}
		if(sign <= 0){
			alert("请选择待操作的项！");
			return false;
		}else if(sign > 1){
			alert("只能选择一个待操作项！");
			return false;
		}
		return true;
	}

	function back(){
		window.location.href = "./contract.do";
		window.location.target = "main";
	}
	
	function advancedQueryDialog(){
		var url = "contractAdvancedAuery.html";
		var dlgParam = "dialogHeight:360px;dialogWidth:450px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		window.showModalDialog(url,"",dlgParam);
	}
	
	function updateBackCost(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			window.location.href = "./contractBackCost.do?action=preUpdateBackCost&id="+id+"&sign=3";
			window.location.target = "main";
		}
	}
	
	function searchContract(){
		var customerName = document.getElementById("customerName");
		if(customerName.value == ""){
			alert("<fmt:message key='openSea.message.search'/>");
		}else{
			document.getElementById("sFlag").value = "1";
			document.deptContractForm.action = "./contract.do?action=preDeptContract";
			document.deptContractForm.target = "main";
			document.deptContractForm.submit();
		}
	}
	
	function pageContract(flag){
		document.deptContractForm.action = "./contract.do?action=preDeptContract&flag="+flag;
		document.deptContractForm.target = "main";
		document.deptContractForm.submit();
	}
	
	function initPage(){
		if("<c:out value='${result}'/>" == 2){
			alert("<fmt:message key='common.message.updateSuccess'/>");
		}
	}
	function advancedQueryDialog(){
		var url = "./contract.do?action=advancedQueryContract&radom="+<%=r.nextInt(100)%>;
		var dlgParam = "dialogHeight:360px;dialogWidth:450px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){		
			//根据从子窗口得到的值，从中提取contractCode,customerName,startContractCost,endContractCost,
			//purchaseDesc,startContractDate,endContractDate
			var tStr = rt.split(",");
			document.getElementById("contractCode").value = tStr[0];										
			document.getElementById("customerName2").value = tStr[1];
			document.getElementById("startContractCost").value = tStr[2];
			document.getElementById("endContractCost").value = tStr[3];
			document.getElementById("purchaseDesc").value = tStr[4];
			document.getElementById("startContractDate").value = tStr[5];
			document.getElementById("endContractDate").value = tStr[6];
			document.getElementById("startBackCost").value = tStr[7];
			document.getElementById("endBackCost").value = tStr[8];
			document.getElementById("invoiceDesc").value = tStr[9];
			document.getElementById("salePersonId").value = tStr[10];
			
			document.getElementById("sFlag").value = "2";
			document.deptContractForm.action = "./contract.do?action=preDeptContract";
			document.deptContractForm.target = "main";
			document.deptContractForm.submit();
		}
	}

	function activationContract(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			window.location.href = "./contract.do?action=activationContract&id="+id+"&sign=2";
			window.location.target = "main";
		}
		}

	function deleteContract(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			window.location.href = "./contract.do?action=deleteContract&id="+id+"&sign=2";
			window.location.target = "main";
		}
	}
	
	function insertBackCost()
	{
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			window.location.href = "./contractBackCost.do?action=preInsertBackCost&id="+id+"&sign=3";
			window.location.target = "main";
		}		
	}
	
	function addInvoice(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			window.location.href = "./contract.do?action=preAddInvoice&id="+id;
			window.location.target = "main";
		}
	}	
	
	function addReverseInvoice(){
		if(checkForm2()){
			var objs = document.getElementsByName("id");
			var id = 0;
			for(var i = 0; i < objs.length; i++){
				if(objs[i].value != 0 && objs[i].checked){
					id = objs[i].value;
					break;
				}
			}
			window.location.href = "./contract.do?action=preAddReverseInvoice&id="+id;
			window.location.target = "main";
		}
	}		
	
-->
</script>
</head>
<body>
<form name="deptContractForm" action="" method="post">
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab">部门合同</td>
					</tr>
					<tr>
						<td valign="bottom" class="a_tools" style="padding-left: 3px;">客户关键字：
							<input id="customerName" name="customerName" class="input_query" type="text" value="${customerName}"/>
							<input id="sFlag" name="sFlag" type="hidden" value="${sFlag}"/>
							<a class="search" href="javascript:searchContract();" title="" hidefocus="true">查询</a>
							|<a class="asearch" href="javascript:advancedQueryDialog();" target="main" hidefocus="true">高级查询</a>
							<!--  暂时功能  -->
							<c:if test="${isUpdateDeptBackcost == 1}">
							|
							<a class="invoice" href="javascript:addInvoice();" title="开票" hidefocus="true" >开票</a>
							|
							<a class="invoice" href="javascript:addReverseInvoice();" title="开票" hidefocus="true" >开票红冲</a>							
							|
							<a class="update" href="javascript:insertBackCost();" title="新增回款" hidefocus="true">新增回款</a>
							|
							<a class="update" href="javascript:updateBackCost();" title="修改回款" hidefocus="true">修改回款</a>
							</c:if>
							   |<c:if test="${userId == 2}">
							<a class="update" href="javascript:activationContract();" title="激活合同" hidefocus="true">激活合同</a>
							</c:if>
							
							<c:if test="${userId == 2}">
							<a class="update" href="javascript:deleteContract();" title="删除合同" hidefocus="true">删除合同</a>
							</c:if>
						</td>
						<!-- 
						<td valign="bottom" align="right">
							<a href="javascript:back();" title="返回" hidefocus="true">返回</a>&nbsp;&nbsp;
						</td>
						 -->
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td class="title_lab">部门合同</td>
						<td align="right" valign="">
							<!-- page -->
							<table class="table_page2">
								<tr>
									<td valign="bottom">
										<img src="./images/frist.bmp" onclick="pageContract(1)" alt="首页" style="vertical-align: middle;cursor: hand;">
										<img src="./images/last.bmp" onclick="pageContract(2)" alt="上一页" style="vertical-align: middle;cursor: hand;">&nbsp;
										<font color="red">${currentPage}</font>/${totalPages}&nbsp;<fmt:message key='common.page'/>&nbsp;
										<img src="./images/next.bmp" onclick="pageContract(3)" alt="下一页" style="vertical-align: middle;cursor: hand;">
										<img src="./images/end.bmp" onclick="pageContract(4)" alt="末页" style="vertical-align: middle;cursor: hand;">
										共&nbsp;&nbsp;${totalRows}&nbsp;<fmt:message key='common.page.totalRow'/>&nbsp;&nbsp;&nbsp;
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr><td colspan="3" height="2"></td></tr>
					<tr>
						<td valign="top" colspan="2">
							<table class="table">
								<tr>
									<td valign="top">
										<table class="table_style">
											<tr>
												<td class="title" width="8"></td>
												<td class="title" width="30">选择</td>
												<td class="title">合同编号</td>
												<td class="title">合同名称</td>
												<td class="title">状态</td>
												<td class="title">合同金额</td>
												<td class="title">回款金额</td>
												<td class="title">应收账款</td>
												<td class="title">申请人</td>
												<td class="title">签约日期</td>
												<td class="title">建档日期</td>
												<!-- 
												<td class="title">客户名称</td>
												 -->
											</tr>
											<c:forEach var="contract" items="${deptContracts}">
											<tr onmouseover="onmousemoveChange(this,'img${contract.id}')" onmouseout="onmouseoutChange(this,'img${contract.id}')">
												<td height="16" width="8" align="right"><img id="img${contract.id}" src="./images/table_sign2.bmp"></td>
												<td align="center">
													<input id="id" name="id" type="checkbox" value="${contract.id}"/>
												</td>
												<td><a href="./contract.do?action=contractDetail&sign=5&id=${contract.id}" target="main">${contract.contractCode}</a></td>
												<td align="left">${contract.contractName}</td>
												<td>
												<c:choose>
													<c:when test="${contract.status == 1}">待审核</c:when>
													<c:when test="${contract.status == 2}">执行中</c:when>
													<c:when test="${contract.status == 3}">退回</c:when>
													<c:when test="${contract.status == 4}">已关闭</c:when>
												</c:choose>
												</td>
												<td>${contract.contractCost}</td>
												<td>${contract.backCost}</td>
												<td>${contract.receivable}</td>
												<td>${contract.salePerson}</td>
												<td><fmt:formatDate value="${contract.contractDate}" pattern="yyyy-MM-dd"/></td>
												<td><fmt:formatDate value="${contract.createDate}" pattern="yyyy-MM-dd"/></td>
												<!-- 
												<td align="left">${contract.customerName}</td>
												 -->
											</tr>
											</c:forEach>
										</table>
									</td>
								</tr>
								<tr>
									<td align="left" valign="bottom">
										<!-- page -->
										<table id="tp" class="table_page">
											<tr>
												<td>&nbsp;&nbsp;
													<img src="./images/frist.bmp" onclick="pageContract(1)" alt="首页" style="vertical-align: middle;cursor: hand;">
													<img src="./images/last.bmp" onclick="pageContract(2)" alt="上一页" style="vertical-align: middle;cursor: hand;">&nbsp;
													<font color="red">${currentPage}</font>/${totalPages}&nbsp;<fmt:message key='common.page'/>&nbsp;
													<img src="./images/next.bmp" onclick="pageContract(3)" alt="下一页" style="vertical-align: middle;cursor: hand;">
													<img src="./images/end.bmp" onclick="pageContract(4)" alt="末页" style="vertical-align: middle;cursor: hand;">
													共&nbsp;&nbsp;${totalRows}&nbsp;<fmt:message key='common.page.totalRow'/>
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<input id="contractCode" name="contractCode" type="hidden" value="${queryContract.contractCode}"/>
	<input id="customerName2" name="customerName2" type="hidden" value="${queryContract.customerName2}"/>
	<input id="startContractCost" name="startContractCost" type="hidden" value="${queryContract.startContractCost}"/>
	<input id="endContractCost" name="endContractCost" type="hidden" value="${queryContract.endContractCost}"/>
	<input id="purchaseDesc" name="purchaseDesc" type="hidden" value="${queryContract.purchaseDesc}"/>
	<input id="startContractDate" name="startContractDate" type="hidden" value="${queryContract.startContractDate}"/>
	<input id="endContractDate" name="endContractDate" type="hidden" value="${queryContract.endContractDate}"/>
	<input id="startBackCost" name="startBackCost" type="hidden" value="${queryContract.startBackCost}"/>
	<input id="endBackCost" name="endBackCost" type="hidden" value="${queryContract.endBackCost}"/>
	<input id="invoiceDesc" name="invoiceDesc" type="hidden" value="${queryContract.invoiceDesc}"/>
	<input id="salePersonId" name="salePersonId" type="hidden" value="${queryContract.salePersonId }"/>
	
	<input id="sFlag" name="sFlag" type="hidden" value="${sFlag}"/>
</form>
</body>
</html>